package cc.comac.mom.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cc.comac.mom.api.QualityFrrFormService;
import cc.comac.mom.entity.QualityFrrFormEntity;
import cc.comac.mom.entity.QualityQarFrrRelationEntity;

@RestController
@RequestMapping("/QualityFrrFormController")
public class QualityFrrFormController {

	@Autowired
	private QualityFrrFormService qualityFrrFormService;
	@RequestMapping(value = "/reciveFrrPartData",method = RequestMethod.POST)
	public String reciveFrrPartData(@RequestBody String qualityFrrFormEntityString) throws Exception {
		 ObjectMapper objectMapper = new ObjectMapper();
		 QualityFrrFormEntity readValue = objectMapper.readValue(qualityFrrFormEntityString, QualityFrrFormEntity.class);
			String reciveFrrPartData = qualityFrrFormService.reciveFrrPartData(readValue);
		return reciveFrrPartData;
	}
	
	/**
	 * FRR系统接收QAR发送的信息进行FRR任务的开立，
	 * 并将FRR编号对应QAR编号进行回传
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 * **/
	@RequestMapping(value = "/openFrr",method = RequestMethod.POST)
	public String openFrr(@RequestBody String QualityQarFrrRelationEntityString) throws JsonParseException, JsonMappingException, IOException {
		 ObjectMapper objectMapper = new ObjectMapper();
		 QualityQarFrrRelationEntity readValue = objectMapper.readValue(QualityQarFrrRelationEntityString, QualityQarFrrRelationEntity.class);
		String openFrr = qualityFrrFormService.openFrr(readValue);
		return openFrr;
	}
	
	/**
	 * 当由QAR触发生成的FRR归档或作废后，
	 * FRR将该状态信息发送给QAR系统
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 * **/
	@RequestMapping(value = "/frrToFileOrScrap",method = RequestMethod.POST)
	public String frrToFileOrScrap(@RequestBody String QualityQarFrrRelationEntityString) throws JsonParseException, JsonMappingException, IOException {
		 ObjectMapper objectMapper = new ObjectMapper();
		 QualityQarFrrRelationEntity readValue = objectMapper.readValue(QualityQarFrrRelationEntityString, QualityQarFrrRelationEntity.class);
		String frrToFileOrScrap = qualityFrrFormService.frrToFileOrScrap(readValue);
		return frrToFileOrScrap;
	}
}
